package com.bytz.modules.cms.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.order.constant.OrderConstants;
import com.bytz.modules.cms.order.entity.Order;
import com.bytz.modules.cms.order.entity.OrderArrival;
import com.bytz.modules.cms.order.mapper.OrderArrivalMapper;
import com.bytz.modules.cms.order.service.IAdminOrderArrivalService;
import com.bytz.modules.cms.order.service.IOrderService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminOrderArrivalServiceImpl extends MPJBaseServiceImpl<OrderArrivalMapper, OrderArrival> implements IAdminOrderArrivalService {
    @Autowired
    private IOrderService orderService;

    @Transactional
    @Override
    public void addArrivalStatus(OrderArrival orderArrival) {
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(Order::getContractNumber, orderArrival.getContractNumber());
        Order order = orderService.getOne(orderLambdaQueryWrapper);
        if (order == null) {
            throw new BytzBootException("该合同编号不存在");
        } else if (!order.getStatus().equals(OrderConstants.OrderStatus.PAI_CHAN_ZHONG)) {
            throw new BytzBootException("只能新增“产品排产中”的订单");
        }
        order.setStatus(OrderConstants.OrderStatus.DAI_TI_JIAO_WEI_KUAN);
        order.setArrivalStatus(orderArrival.getArrivalStatus());
        //向order表中更改信息
        orderService.updateArrivalStatusAndStatus(order.getId(), order.getStatus(), order.getArrivalStatus());
        //向OrderArrival表中插入信息
        this.save(orderArrival);
    }

    @Transactional
    @Override
    public void updateArrivalStatus(OrderArrival orderArrival) {
        orderService.updateArrivalStatus(orderArrival.getArrivalStatus(), orderArrival.getContractNumber());
        this.updateById(orderArrival);
    }

    @Transactional
    @Override
    public void importExcel(List<OrderArrival> importData) {
        importData.forEach(this::checkImportData);
        //导入前清空数据库
        this.remove(null);
        LocalDateTime now = LocalDateTime.now();
        importData.forEach(orderArrival -> orderArrival.setCreateTime(now));
        //向order_arrival表中插入数据
        this.saveBatch(importData);
        //向order表联查，查询哪些编号不存在且不是排产状态的订单
        MPJLambdaWrapper<OrderArrival> orderArrivalMPJLambdaWrapper = new MPJLambdaWrapper<OrderArrival>()
                .select(OrderArrival::getContractNumber)
                .leftJoin(Order.class, Order::getContractNumber, OrderArrival::getContractNumber)
                .isNull(Order::getId)
                .or()
                .notIn(Order::getStatus,
                        OrderConstants.OrderStatus.YI_YU_FU,
                        OrderConstants.OrderStatus.PAI_CHAN_ZHONG,
                        OrderConstants.OrderStatus.DAI_TI_JIAO_WEI_KUAN);
        List<String> errorContractNumber = baseMapper.selectJoinList(String.class, orderArrivalMPJLambdaWrapper);
        if (errorContractNumber.size() > 0) {
            throw new BytzBootException("以下合同编号不存在或订单状态不为已预付、排产中、待提交尾款:" + StrUtil.join(", ", errorContractNumber));
        }
        //将order表的到货情况置为空
        LambdaUpdateWrapper<Order> orderLambdaUpdateWrapper = new LambdaUpdateWrapper<Order>()
                .set(Order::getArrivalStatus, null);
        orderService.update(orderLambdaUpdateWrapper);
        //联表更新,将order_arrival表中的到货情况同步到order表的到货情况
        baseMapper.updateOrderJoinOrderArrival();
        //修改order表中的合同状态
        LambdaUpdateWrapper<Order> orderLambdaUpdateWrapper1 = new LambdaUpdateWrapper<Order>()
                .set(Order::getStatus, OrderConstants.OrderStatus.DAI_TI_JIAO_WEI_KUAN)
                .in(Order::getStatus, OrderConstants.OrderStatus.YI_YU_FU, OrderConstants.OrderStatus.PAI_CHAN_ZHONG)
                .isNotNull(Order::getArrivalStatus);
        orderService.update(orderLambdaUpdateWrapper1);
    }

    /**
     * 检查解析后的Excel_List数据
     */
    private void checkImportData(OrderArrival orderArrival) {
        if (StringUtils.isBlank(orderArrival.getContractNumber())) {
            throw new BytzBootException("该Excel表里有合同编号未填,导入失败");
        }
    }

}
